﻿using System;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using XLua;

namespace GameCore
{
    [LuaCallCSharp]
    public partial class UIManager : SingletonWhereMono<UIManager>
    {
        private List<PanelData> panelDataList;
        private Transform bottomLayerTr, normalLayerTr, topLayerTr;
        private Transform panelPool;
        
        private Dictionary<string, LuaBehaviourFormUiPanel> uiDict;

        public void Awake()
        {
            bottomLayerTr = transform.Find("Canvas/Bottom");
            normalLayerTr = transform.Find("Canvas/Normal");
            topLayerTr = transform.Find("Canvas/Top");
            panelPool = transform.Find("PanelPool");
            
            uiDict = new Dictionary<string, LuaBehaviourFormUiPanel>();
            
            AssetsManager.Instance.LoadAsset<TextAsset>("Assets/GameRes/Tables/UI.json.txt", (panelDataJson) =>
            {
                panelDataList = JsonMapper.ToObject<List<PanelData>>(panelDataJson.text);
            });
        }

        public void GetPanel(string panelName, Action<LuaBehaviourFormUiPanel> onSucceed = null, Action<string> onFailed = null)
        {
            var panelData = panelDataList.Find(p => p.PanelName == panelName);
            
            LuaBehaviourFormUiPanel panel = null;
            if(uiDict.TryGetValue(panelName, out panel))
            {
                if (panel.IsShowing)
                {
                    onFailed?.Invoke("面版正在显示中, 重复加载.");
                    return;
                }
                
                switch (panelData.Layer)
                {
                    case E_PanelLayer.Bottom:
                    {
                        panel.transform.SetParent(bottomLayerTr);
                        break;
                    }
                    case E_PanelLayer.Normal:
                    {
                        panel.transform.SetParent(normalLayerTr);
                        break;
                    }
                    case E_PanelLayer.Top:
                    {
                        panel.transform.SetParent(topLayerTr);
                        break;
                    }
                }
                
                panel.OnShow();
                onSucceed?.Invoke(panel);
                return;
            }

            AssetsManager.Instance.LoadAsset<LuaBehaviourFormUiPanel>(panelData.LoadPath, (panelPrefab) =>
            {
                if(panelPrefab == null)
                {
                    onFailed?.Invoke("加载面板预制体失败.");
                    return;
                }
                
                panel = null;
                switch (panelData.Layer)
                {
                    case E_PanelLayer.Bottom:
                    {
                        panel = Instantiate(panelPrefab, bottomLayerTr);
                        break;
                    }
                    case E_PanelLayer.Normal:
                    {
                        panel = Instantiate(panelPrefab, normalLayerTr);
                        break;
                    }
                    case E_PanelLayer.Top:
                    {
                        panel = Instantiate(panelPrefab, topLayerTr);
                        break;
                    }
                }
                panel!.gameObject.name = panelName;
                
                panel.OnInit();
                panel.OnShow();
                onSucceed?.Invoke(panel);
                
                uiDict.Add(panelName, panel);
            });
        }

        public void HidePanel(string panelName)
        {
            if (uiDict.TryGetValue(panelName, out var panel))
            {
                if (!panel.IsShowing)
                {
                    Debug.LogError("面板没有显示, 或者已经被回收.");
                    return;
                }
                
                panel.transform.SetParent(panelPool);
                panel.OnHide();
                return;
            }
            
            Debug.LogError("面板从未被呼出.");
        }
    }
}
